package org.xinjiang.data.migration.flush.convert;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.xinjiang.data.migration.hainan.entity.SysUser;
import org.xinjiang.data.migration.hainan.entity.SysUserDepart;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author: 邢卫
 * @date: 2023年12月17日 10:41
 */
@Component
public class SysUserDepartConvert extends BaseConvert<SysUser, SysUserDepart> {
    @Override
    public SysUserDepart convert(SysUser sysUser) {
        SysUserDepart sysUserDepart = new SysUserDepart();
        sysUserDepart.setUserId(sysUser.getId());
        sysUserDepart.setDepId(sysUser.getDepartIds());
        return sysUserDepart;
    }

    private SysUserDepart convertIgds(String userId, String departId) {
        SysUserDepart sysUserDepart = new SysUserDepart();
        sysUserDepart.setUserId(userId);
        sysUserDepart.setDepId(departId);
        return sysUserDepart;
    }

    @Override
    public Collection<SysUserDepart> converts(Collection<SysUser> ss) {
        Collection<SysUserDepart> data = new ArrayList<>();
        for (SysUser s : ss) {
            String departIds = s.getDepartIds();
            if (StringUtils.isEmpty(departIds)) {
                continue;
            }
            if (departIds.contains(",")) {
                for (String s1 : departIds.split(",")) {
                    data.add(convertIgds(s.getId(), s1));
                }
            } else if (departIds.contains(";")) {
                for (String s1 : departIds.split(";")) {
                    data.add(convertIgds(s.getId(), s1));
                }
            } else {
                data.add(convert(s));
            }
        }
        return data;
    }
}
